package cn.hpclub.server.interceptor;

import cn.hpclub.server.security.TokenUtils;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.handler.Handler;
import com.jfinal.kit.HandlerKit;
import com.jfinal.kit.StrKit;
import com.jfinal.render.RenderFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wanglin on 2017/7/19.
 */
public class HtmlHandler extends Handler {


    @Override
    public void handle(String target, HttpServletRequest request,
                       HttpServletResponse response, boolean[] isHandled) {
        if (!target.contains("ccbresult.html")
                && (StrKit.notBlank(target) && target.contains(".html"))) {//放开建行回调页面过滤

            String saleType = request.getParameter("saleType");
            if (StrKit.notBlank(saleType) &&(saleType.equals("1") || saleType.equals("3"))) {//saleType=1或者3
                if (null != TokenUtils.validToken(request.getParameter("token"))) {//token失效
                    renderError401(request, response, isHandled);
                }
            }
            nextHandler.handle(target, request, response, isHandled);
        } else {
            nextHandler.handle(target, request, response, isHandled);
        }
    }

    private static void renderError401(HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        isHandled[0] = true;
        RenderFactory.me().getErrorRender(401).setContext(request, response).render();
    }
}
